﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Snake.Net;

namespace Snake
{
    public partial class Frm_BarrierPast : Form
    {
        string username;
        int barrier = 0;
        bool b1 = false;
        bool b2 = false;
        bool b3 = false;
        int page = 0;
        int allpage = 0;//获取文件总数，为 （总数-1）/3；
        int allfile = 0;
        Graphics g;

        Rectangle lastpage = new Rectangle(40, 480, 200, 80);
        Rectangle nextpage = new Rectangle(520, 480, 200, 80);
        Rectangle begin = new Rectangle(40, 600, 200, 80);
        Rectangle exit = new Rectangle(520, 600, 200, 80);
        Rectangle chosebarrier = new Rectangle(-5, 160, 800, 80);
        Rectangle left = new Rectangle(40, 240, 200, 200);
        Rectangle medium = new Rectangle(280, 240, 200, 200);
        Rectangle right = new Rectangle(520, 240, 200, 200);
        Rectangle maxscore = new Rectangle(280, 480, 200, 80);
        Rectangle score = new Rectangle(280, 600, 200, 80);
        Rectangle titler = new Rectangle(0, 0, 760, 160);

        StringFormat stringFormat = new StringFormat();
        Font buttonfont = new Font("黑体", 40);
        Font title = new Font("黑体", 80);


        private ButtonType button = ButtonType.None;

        public Frm_BarrierPast()
        {
            InitializeComponent();
            Size = new Size(760, 800);
            allfile = HttpRequest.GetBarrierCount();
            allpage = (allfile - 1) / 3;
        }
        
        public Frm_BarrierPast(string name)
        {
            InitializeComponent();
            Size = new Size(760, 800);
            allfile = HttpRequest.GetBarrierCount();
            allpage = allfile / 3;
            username = name;
        }

        private void Frm_Barrier_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void Frm_Barrier_MouseMove(object sender, MouseEventArgs e)
        {
            ButtonType jugearea()
            {
                if (e.X > lastpage.X && e.X < lastpage.X + lastpage.Width &&
                       e.Y > lastpage.Y && e.Y < lastpage.Y + lastpage.Height)
                    return ButtonType.Last;
                else if (e.X > nextpage.X && e.X < nextpage.X + nextpage.Width &&
                    e.Y > nextpage.Y && e.Y < nextpage.Y + nextpage.Height)
                    return ButtonType.Next;
                else if (e.X > begin.X && e.X < begin.X + begin.Width &&
                    e.Y > begin.Y && e.Y < begin.Y + begin.Height)
                    return ButtonType.Start;
                else if (e.X > exit.X && e.X < exit.X + exit.Width &&
                    e.Y > exit.Y && e.Y < exit.Y + exit.Height)
                    return ButtonType.Quit;
                else if (e.X > maxscore.X && e.X < maxscore.X + maxscore.Width &&
                    e.Y > maxscore.Y && e.Y < maxscore.Y + maxscore.Height)
                    return ButtonType.MaxScore;
                else if (e.X > left.X && e.X < left.X + left.Width &&
                    e.Y > left.Y && e.Y < left.Y + left.Height)
                    return ButtonType.B1;
                else if (e.X > right.X && e.X < right.X + right.Width &&
                    e.Y > right.Y && e.Y < right.Y + right.Height)
                    return ButtonType.B3;
                else if (e.X > medium.X && e.X < medium.X + medium.Width &&
                    e.Y > medium.Y && e.Y < medium.Y + medium.Height)
                    return ButtonType.B2;
                else
                    return ButtonType.None;
            }

            using(var penred = new Pen(Color.Red, 5f))
            {
                
                switch (jugearea())
                {
                    case ButtonType.B1:
                        if (button != ButtonType.B1)
                            g.DrawRectangle(penred, left);
                        button = jugearea();
                        break;
                    case ButtonType.B2:
                        if (button != ButtonType.B2)
                            g.DrawRectangle(penred, medium);
                        button = jugearea();
                        break;
                    case ButtonType.B3:
                        if (button != ButtonType.B3)
                            g.DrawRectangle(penred, right);
                        button = jugearea();
                        break;
                    case ButtonType.Last:
                        if (button != ButtonType.Last)
                            g.DrawRectangle(penred, lastpage);
                        button = jugearea();
                        break;
                    case ButtonType.MaxScore:
                        if (button != ButtonType.MaxScore)
                            g.DrawRectangle(penred, maxscore);
                        button = jugearea();
                        break;
                    case ButtonType.Next:
                        if (button != ButtonType.Next)
                            g.DrawRectangle(penred, nextpage);
                        button = jugearea();
                        break;
                    case ButtonType.Quit:
                        if (button != ButtonType.Quit)
                            g.DrawRectangle(penred, exit);
                        button = jugearea();
                        break;
                    case ButtonType.Start:
                        if (button != ButtonType.Start)
                            g.DrawRectangle(penred, begin);
                        button = jugearea();
                        break;
                    case ButtonType.None:
                        if (button != ButtonType.None)
                        {
                            using (var penwhite = new Pen(Color.DarkGray, 5f))
                            {
                                if (!b1)
                                    g.DrawRectangle(penwhite, left);
                                if (!b2)
                                    g.DrawRectangle(penwhite, medium);
                                if (!b3)
                                    g.DrawRectangle(penwhite, right);
                                g.DrawRectangle(penwhite, lastpage);
                                g.DrawRectangle(penwhite, nextpage);
                                g.DrawRectangle(penwhite, begin);
                                g.DrawRectangle(penwhite, exit);
                                g.DrawRectangle(penwhite, maxscore);
                            }
                            button = ButtonType.None;
                        }
                        break;
                }
            }
        }

        public void Init()
        {
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            g.Clear(Color.LightBlue);

            using (var penwhite = new Pen(Color.DarkGray, 5f))
            {
                Brush b = Brushes.White;
                Font bf = buttonfont;
                StringFormat sf = stringFormat;
                g.DrawRectangle(penwhite, begin);
                g.DrawRectangle(penwhite, exit);
                g.DrawRectangle(penwhite, lastpage);
                g.DrawRectangle(penwhite, nextpage);
                g.DrawRectangle(penwhite, maxscore);
                g.DrawRectangle(penwhite, score);
                g.DrawRectangle(penwhite, chosebarrier);
                g.DrawRectangle(penwhite, left);
                g.DrawRectangle(penwhite, medium);
                g.DrawRectangle(penwhite, right);

                g.DrawString("开始", bf, b, begin, sf);
                g.DrawString("退出", bf, b, exit, sf);
                g.DrawString("下一页", bf, b, nextpage, sf);
                g.DrawString("上一页", bf, b, lastpage, sf);
                g.DrawString("排行榜", bf, b, maxscore, sf);
                g.DrawString("本地", bf, b, score, sf);
                g.DrawString("选择关卡", bf, b, chosebarrier, sf);
                g.DrawString("贪吃蛇", title, Brushes.Green, titler, sf);
                //此处三个draw 后期全部替换为预览,带有//标记
                g.DrawString("第" + (page * 3 + 1).ToString() + "关", bf, b, left, sf);//
                g.DrawString("第" + (page * 3 + 2).ToString() + "关", bf, b, medium, sf);//
                g.DrawString("第" + (page * 3 + 3).ToString() + "关", bf, b, right, sf);//
            }  
        }


        enum ButtonType
        {
            None,
            Start,
            Quit,
            B1,
            B2,
            B3,
            Next,
            Last,
            MaxScore,
        }

        private void Frm_Barrier_Shown(object sender, EventArgs e)
        {
            g = CreateGraphics();
            Init();
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void gPanelTitleBack_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void Frm_Barrier_MouseDown(object sender, MouseEventArgs e)
        {
            //暂时处理方案重绘关卡显示
            Brush b = Brushes.White;
            Font bf = buttonfont;
            StringFormat sf = stringFormat;

            if (e.X > lastpage.X && e.X < lastpage.X + lastpage.Width &&
                       e.Y > lastpage.Y && e.Y < lastpage.Y + lastpage.Height)
            {
                if (page > 0)
                {
                    page--;
                    Init();
                }

            }
            else if (e.X > nextpage.X && e.X < nextpage.X + nextpage.Width &&
                e.Y > nextpage.Y && e.Y < nextpage.Y + nextpage.Height)
            {
                //获取文件数目；if(page<文件数目/3)
                if (page < allpage)
                {
                    page++;
                    Init();
                }

            }
            else if (e.X > begin.X && e.X < begin.X + begin.Width &&
                e.Y > begin.Y && e.Y < begin.Y + begin.Height)
            {
                Hide();
                if (barrier == 0)
                    barrier = page * 3 + 1;
                new Frm_Game(barrier, username).ShowDialog();
                Show();
                Frm_Barrier_Shown(sender, e);
            }
            else if (e.X > exit.X && e.X < exit.X + exit.Width &&
                e.Y > exit.Y && e.Y < exit.Y + exit.Height)
            {
                Environment.Exit(0);
            }
            else if (e.X > maxscore.X && e.X < maxscore.X + maxscore.Width &&
                e.Y > maxscore.Y && e.Y < maxscore.Y + maxscore.Height)
            {
                //排行榜
                //做成绩储存
            }
            else if (e.X > left.X && e.X < left.X + left.Width &&
                e.Y > left.Y && e.Y < left.Y + left.Height)
            {
                b1 = true;
                b2 = false;
                b3 = false;
                barrier = page * 3 + 1;
            }
            else if (e.X > medium.X && e.X < medium.X + medium.Width &&
                e.Y > medium.Y && e.Y < medium.Y + medium.Height)
            {
                b1 = false;
                b2 = true;
                b3 = false;
                barrier = page * 3 + 2;
            }
            else if (e.X > right.X && e.X < right.X + right.Width &&
                e.Y > right.Y && e.Y < right.Y + right.Height)
            {
                b1 = false;
                b2 = false;
                b3 = true;
                barrier = page * 3 + 3;
            }
            Frm_Barrier_MouseMove(sender, e);
            gPanelTitleBack_MouseDown(this, e);
        }
        
    }
}
